#coding=utf-8
import sqlite3


def dict_factory(cursor, row):
    d = {}
    for idx,col in enumerate(cursor.description):
        d[col[0]] = row[idx]
    return d

class SQLite(object):
    def __init__(self, dbPath):
        self.dbPath = dbPath
        self.conn = self.conn()
        self.cursor = self.cursor()
        self.conn.text_factory = str

    def conn(self):
        conn = sqlite3.connect(self.dbPath)
        conn.row_factory = dict_factory
        return conn
    
    def cursor(self):
        return self.conn.cursor()
 
    def execute(self, sql, flag=1):
        self.cursor.execute(sql)
        if flag == 1:
            self.commit()
    
    def fetchall(self, sql):
        self.execute(sql)
        return self.cursor.fetchall()

    def fetchone(self, sql):
        self.execute(sql)
        return self.cursor.fetchone()
    
    def lastId(self):
        return self.cursor.lastrowid

    def commit(self):
        self.conn.commit()

if __name__ == '__main__':
    dbPath = './test.db'
    db = SQLite(dbPath)

    #sql = "INSERT INTO catalog (listid,name,url)VALUES('%s','%s','%s')"\
    #  %('12','leonffgod','http://baidu.com')
    #db.execute(sql)
    sql = "select * from catalog"
    print db.fetchall(sql)
'''
db = SQLite(dbPath)

sql = "INSERT INTO catalog (listid,name,url)VALUES('%s','%s','%s')"\
      %('12','leon-gggod','http://baidu.com')
db.execute(sql)
conn.execute("select * from catalog");
print conn.fetchall()
'''